package com.ruoyi.alarm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.alarm.domain.AlarmWarning;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.push.PushUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.alarm.mapper.AlarmPushMapper;
import com.ruoyi.alarm.domain.AlarmPush;
import com.ruoyi.alarm.service.IAlarmPushService;

import javax.annotation.PostConstruct;

/**
 * 告警推送Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-03-10
 */
@Service
public class AlarmPushServiceImpl implements IAlarmPushService 
{
    public static final String CACHE_MAP = "alarm_push";
    private static final Logger log = LoggerFactory.getLogger(AlarmPushServiceImpl.class);

    @Autowired
    private AlarmPushMapper alarmPushMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }


    /**
     * 查询告警推送
     *
     * @param alarmType 告警类型
     * @return 告警推送
     */
    public List<AlarmPush> selectAlarmPushByType(String alarmType){
        List<AlarmPush> list = redisCache.getCacheMapValue(CACHE_MAP, alarmType);
        if(list != null){
            return list;
        }

        AlarmPush pc = new AlarmPush();
        pc.setAlarmType(alarmType);
        list = alarmPushMapper.selectAlarmPushList(pc);
        if(list != null && !list.isEmpty()){
            redisCache.setCacheMapValue(CACHE_MAP, alarmType, list);
        }

        return list;
    }

    /**
     * 查询告警推送
     * 
     * @param id 告警推送主键
     * @return 告警推送
     */
    @Override
    public AlarmPush selectAlarmPushById(Long id)
    {
        return alarmPushMapper.selectAlarmPushById(id);
    }

    /**
     * 查询告警推送列表
     * 
     * @param alarmPush 告警推送
     * @return 告警推送
     */
    @Override
    public List<AlarmPush> selectAlarmPushList(AlarmPush alarmPush)
    {
        return alarmPushMapper.selectAlarmPushList(alarmPush);
    }

    /**
     * 新增告警推送
     * 
     * @param alarmPush 告警推送
     * @return 结果
     */
    @Override
    public int insertAlarmPush(AlarmPush alarmPush)
    {
        alarmPush.setCreateTime(DateUtils.getNowDate());
        return alarmPushMapper.insertAlarmPush(alarmPush) + reCache();
    }

    /**
     * 修改告警推送
     * 
     * @param alarmPush 告警推送
     * @return 结果
     */
    @Override
    public int updateAlarmPush(AlarmPush alarmPush)
    {
        alarmPush.setUpdateTime(DateUtils.getNowDate());
        return alarmPushMapper.updateAlarmPush(alarmPush) + reCache();
    }

    /**
     * 批量删除告警推送
     * 
     * @param ids 需要删除的告警推送主键
     * @return 结果
     */
    @Override
    public int deleteAlarmPushByIds(Long[] ids)
    {
        return alarmPushMapper.deleteAlarmPushByIds(ids) + reCache();
    }

    /**
     * 删除告警推送信息
     * 
     * @param id 告警推送主键
     * @return 结果
     */
    @Override
    public int deleteAlarmPushById(Long id)
    {
        return alarmPushMapper.deleteAlarmPushById(id) + reCache();
    }

    /**
     * 计算告警推送用户
     *
     * @param alarmWarning 告警
     * @return 结果
     */
    @Override
    public List<PushUser> alarmPushUser(AlarmWarning alarmWarning){
        List<PushUser> list = new ArrayList<>();
        List<AlarmPush> pushList = selectAlarmPushByType(alarmWarning.getAlarmType());
        for(AlarmPush alarmPush:pushList){
           List<SysUser> users = null;
            if(alarmPush.getPushTarget().equalsIgnoreCase(AlarmPush.TARGET_ALL)){
                PushUser pushUser = new PushUser();
                pushUser.setPushType(alarmPush.getPushType());
                pushUser.setUserId(0L);
                list.add(pushUser);
            } if(alarmPush.getPushTarget().equalsIgnoreCase(AlarmPush.TARGET_USER)){
                users = sysUserMapper.selectUserListByCondition(" user_id in ( "+alarmPush.getPushUser()+" )");
            }else if(alarmPush.getPushTarget().equalsIgnoreCase(AlarmPush.TARGET_DEPT)){
                if(StringUtils.isEmpty(alarmPush.getPushUser())) {
                    users = sysUserMapper.selectUserListByCondition(" dept_id = " + alarmWarning.getDeptId());
                }else{
                    users = sysUserMapper.selectUserListByCondition(" dept_id in ( "+alarmPush.getPushUser()+" )");
                }
            }else if(alarmPush.getPushTarget().equalsIgnoreCase(AlarmPush.TARGET_POST)){
                users = sysUserMapper.selectUserListByCondition(" user_id in ( select user_id from sys_user_post where post_id in ("+alarmPush.getPushUser()+" ) )");
            }else if(alarmPush.getPushTarget().equalsIgnoreCase(AlarmPush.TARGET_ROLE)){
                users = sysUserMapper.selectUserListByCondition(" user_id in ( select user_id from sys_user_role where role_id in ("+alarmPush.getPushUser()+" ) )");
            }else{
                users = null;
            }
            if(users == null || users.isEmpty()){
                continue;
            }
            for(SysUser user :users){
                PushUser pushUser = new PushUser();
                pushUser.setUserId(user.getUserId());
                pushUser.setPhone(user.getPhonenumber());
                pushUser.setUserName(user.getNickName());
                pushUser.setPushType(alarmPush.getPushType());
                list.add(pushUser);
            }
        }
        return list;
    }

}
